﻿using System.Collections.Generic;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using HashFoo.MigratorFoo;
using HashFoo.Nhib;
using HashFoo.Nhib.Spatial;
using HashFoo.NhibTests.DomainFixtures.Models;
using NHibernate.Cfg;
using NHibernate.Dialect;

namespace HashFoo.NhibTests.DomainFixtures
{
    [DbConfigurationFixture]
    public static class MockConfigurator
    {
        [DbConfiguration("default", true)]
        public static Configuration DefaultConfiguration
        {
            get { return BuildMSSqlConfiguration("Data Source=(local);Initial Catalog=HashFoo.Core.Tests;Integrated Security=True"); }
        }

        [DbConfiguration("test")]
        public static Configuration TestConfiguration
        {
            get { return BuildMSSqlConfiguration("Data Source=(local);Initial Catalog=MigratorFoo.Test;Integrated Security=True"); }
        }

        public static Configuration BuildConfiguration(IPersistenceConfigurer databaseConfig)
        {
            return Fluently.Configure()
                .Database(databaseConfig)
                .Mappings(
                    m => m.AutoMappings.Add(
                        AutoMap
                            .AssemblyOf<FieldBoundary>(new DefaultAutoMapping())
                            .Conventions.AddFromAssemblyOf<HashFooGeographyTypeConvention>()
                            .Conventions.AddFromAssemblyOf<EnumerationTypeConvention>()))
                .ExposeConfiguration(c => c.AddProperties(new Dictionary<string, string> { { "show_sql", "true" } }))
                .BuildConfiguration();
        }

        public static Configuration BuildMSSqlConfiguration(string connectionString)
        {
            return BuildConfiguration(MsSqlConfiguration.MsSql2008
                                          .ConnectionString(
                                              c => c.Is(connectionString))
                                          .Dialect<MsSql2008Dialect>());
        }
    }
}